<template>
  <div class="main">
    <div class="topTitle">{{ props.title }}</div>
    <div class="form_content">
      <NScrollbar>
        <div class="content">
          <NSpace>
            <NForm ref="formRef" require-mark-placement="left" label-placement="left" :rules="rules" :label-width="115"
              :model="formValue" style="width: 800rem" label-align="right">
              <NFormItem>
                <div class="progress">
                  <div style="display:flex; width: auto;padding: 0 15px;">
                    <NIcon :size="20" class="iconStyle">
                      <ApprovalFilled />
                    </NIcon>
                    <span>审批流程</span>
                  </div>
                  <span style="font-size: 18rem;flex:1;" :style="{ color: !workFlowList ? 'red' : '#79bb92' }">
                    {{ workFlowList ? workFlowList : '暂无配置流程，请联系管理员进行配置' }}
                  </span>
                </div>
              </NFormItem>

              <NFormItem :show-feedback="false" v-if="hint">
                <div style="width: 100%; display: flex; align-items: center; margin: 0rem 0 20rem 0">
                  <NIcon size="18">
                    <ErrorRound style="color: #5fb17f" />
                  </NIcon>
                  <div style="margin-left: 10rem;white-space: pre-wrap;" v-html="hint"></div>
                </div>
              </NFormItem>

              <NSpace justify="center">
                <NFormItem label="项目名称:" path="projectName">
                  <NInput placeholder="请输入项目名称" class="commonWidth" v-model:value="formValue.projectName" />
                </NFormItem>
                <NFormItem label="审批号:" path="projectName">
                  <NInput placeholder="请输入审批号" class="commonWidth" v-model:value="formValue.projectName" />
                </NFormItem>
                <NFormItem label="项目编号:" path="projectName">
                  <NInput placeholder="请输入项目编号" class="commonWidth" v-model:value="formValue.projectName" />
                </NFormItem>
                <NFormItem label="项目负责人:" path="projectName">
                  <NInput placeholder="请输入项目负责人" class="commonWidth" v-model:value="formValue.projectName" />
                </NFormItem>
                <NFormItem label="项目预算:" path="budgetAmount">
                  <NInput placeholder="请输入项目预算" class="commonWidth" v-model:value="formValue.budgetAmount" />
                </NFormItem>
                <NFormItem label="采购内容:" path="procurementContent">
                  <NInput placeholder="请输入采购内容" v-model:value="formValue.procurementContent" class="commonWidth"
                    type="textarea" style="height: 130rem" />
                </NFormItem>
              </NSpace>
            </NForm>
          </NSpace>
        </div>
      </NScrollbar>
    </div>
  </div>
  <FixedAction>
    <div style="display: flex">
      <NSpace justify="start" style="flex: 0 0 42%">
        <NButton style="width: 78px; color: #5fb17f" ghost @click="goBack">
          <template #icon>
            <img :src="VBackImg" style="width: 25rem; height: 25rem" />
          </template>
          返回
        </NButton>
      </NSpace>

      <NSpace justify="center">
        <NButton type="primary" style="background-color: #5faab1;" @click="confirm(true)" :loading="loading"
          v-if="workFlowList">
          保存草稿
        </NButton>
        <NButton type="primary"  @click="confirm(false)" :loading="loading" v-if="workFlowList">
          提交申请
        </NButton>
      </NSpace>
    </div>
  </FixedAction>

  <NModal :auto-focus="false" v-model:show="modalVisible">
    <VDialogContent @close="modalVisible = false" @submit="againConfirm" title="保存提示">
      <div style="width: 300rem; height: 100rem; text-align: center; line-height: 100rem">
        {{ contentContent }}
      </div>
    </VDialogContent>
  </NModal>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import {
  NSpace,
  NForm,
  NInput,
  NFormItem,
  NButton,
  NScrollbar,
  useMessage,
  NModal,
  NIcon,
  type FormInst
} from 'naive-ui'
import router from '@/router'
import { useTagStore } from '@/stores/layout'
import FixedAction from '@/components/FixedAction.vue'
import { sendRequest } from '@/utils/api'
import VDialogContent from '@/views/OA/Common/DialogContent.vue'
import { useRoute } from 'vue-router'
import { workFlow } from '@/enums/workFlow'
import VBackImg from '@/assets/images/back.png'
import { ApprovalFilled } from '@vicons/material'
import { ErrorRound } from '@vicons/material'

const route = useRoute()
const tags = useTagStore().tags
const message = useMessage()

const props = defineProps({
  title: { type: String, default: () => '政府立项申请' },
  id: { type: [Number, String], default: () => null },
  flag: { default: () => null }
})

const formValue = reactive<Record<string, any>>({
  draftFlag: null, //草稿状态
  entityId: null, //流程实体id
  projectName: null,
  budgetAmount: null,
  procurementContent: null
})
const rules = {
  projectName: {
    required: true,
    message: '请填写项目名称',
    trigger: ['input', 'blur']
  }
}

// 获取流程id
const hint = ref()
const workFlowList = ref(' ')
const getWorkFlow = async () => {
  const [error, res] = await sendRequest({
    module: 'receiveOrDispatch',
    api: 'workFlow',
    override: {
      params: {
        functionCode:
          props.title == '政府立项申请'
            ? workFlow.ZCLXSQLC
            : props.title == '展览立项申请'
              ? workFlow.ZLLXSQLC
              : workFlow.GCLXSQLC
      }
    }
  })
  if (error) return
  if (res.code !== 0) return
  formValue.entityId = res.data.entityId
  hint.value = res.data.remark
  workFlowList.value = res.data.workFlowList?.length ? res.data.workFlowList?.map((it: any) => it).join(' → ') : null
}
getWorkFlow()

//获取详情
const getInfo = async () => {
  const [error, res] = await sendRequest({
    module: 'procure',
    api: 'procureItem',
    uri: {
      id: props.id as never
    }
  })
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  Object.keys(formValue).map((it: any) => {
    // @ts-ignore
    formValue[it] = res.data[it]
  })
}

// 返回
const goBack = () => {
  const index = tags.findIndex((item: any) => item.path === router.currentRoute.value.fullPath)

  tags.splice(index, 1)
if (tags.length == 0) {
    router.push({ path: '/OA/Workbench/UserDesktop' })
  } else history.back()
}

// 确定
const loading = ref(false)
const formRef = ref<FormInst | null>(null)
const modalVisible = ref(false)
const contentContent = ref()

const confirm = (status: boolean) => {
  formValue.draftFlag = status
  formRef.value?.validate(async (err) => {
    if (err) return
    modalVisible.value = true
    contentContent.value = status ? '您确定要保存草稿吗？' : ' 您确定要提交申请吗？'
  })
}

const againConfirm = async () => {
  if (props.id && !props.flag) {
    const [error, res] = await sendRequest({
      module: 'procure',
      api: 'procureUpdate',
      override: {
        data: {
          ...formValue.value,
          id: props?.id
        }
      }
    })
    if (error) return message.error(error.message)
    if (res.code != 0) return message.error(res.msg)
    message.success(res.msg)
    goSubmit()
  } else {
    const [error, res] = await sendRequest({
      module: 'procure',
      api: 'procureSave',
      override: {
        data: formValue
      }
    })
    if (error) return message.error(error.message)
    if (res.code != 0) return message.error(res.msg)
    message.success(res.msg)
    goSubmit()
  }
  modalVisible.value = false
}

//提交完跳转我的提交
const goSubmit = () => {
  const index = tags.findIndex((item: any) => item.path === router.currentRoute.value.fullPath)
 tags.splice(index, 1)
  if(formValue.draftFlag) return router.push({ path: '/OA/UserCenter/Drafts' })
  router.push({ path: '/OA/UserCenter/Submit' })
}

watch(
  () => props.id,
  () => {
    if ((props.title == '政采立项编辑' ||
      props.title == '工程立项编辑' ||
      props.title == '展览立项编辑') && props.id) getInfo()
  },
  { deep: true, immediate: true }
)
</script>

<style scoped lang="scss">
@mixin CommonFont {
  font-size: 16rem;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}

.commonWidth {
  width: 360rem;
}

.commonWidthA {
  width: 155rem;
}

.main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .topTitle {
    width: 100%;
    height: 64rem;
    border-bottom: 1px solid #fff;
    padding: 20rem;
    margin-bottom: 20rem;
    @include CommonFont;
  }

  .form_content {
    width: 100%;
    height: 100%;
    padding: 0 20rem 0rem;

    .content {
      display: flex;
      align-items: center;
      flex-direction: column;
      width: 100%;
      height: 100%;
      padding: 20rem 0 180rem;
      overflow-y: scroll;
      background-color: #ebeceb;

      .scope {
        display: flex;
        flex-wrap: wrap;
        width: 100%;
        height: 130rem;
        background: #e1e1e1;
        padding: 15rem 20rem;

        .userBox {
          display: flex;
          justify-content: space-between;
          align-items: center;
          width: auto;
          min-width: 150rem;
          height: 40rem;
          background-color: #fff;
          border-radius: 4rem;
          padding: 0 10rem;
          margin-right: 20rem;
        }
      }
    }
  }
}

.iconStyle {
  margin-right: 5rem;
  color: #5fb17f;
}

.progress {
  width: 100%;
  min-height: 50rem;
  background-color: #d5e2d8;
  border-radius: 5rem;
  padding: 10rem;
  display: flex;
  align-items: center;
  margin-top: 10rem;
}

.content::-webkit-scrollbar {
  display: none;
}

:deep(.n-spin-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-content) {
  width: 100%;
  height: 100%;
}

:deep(.n-spin-content) {
  width: 100%;
  height: 100%;
}
</style>
